package com.starry.home.ui.hospital

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.starry.core.base.BaseActivity
import com.starry.core.ext.singleClick
import com.starry.core.ext.toast
import com.starry.core.utils.ZKStatusBarUtil
import com.starry.home.R
import com.starry.home.databinding.ActivityHospitalBinding
import com.starry.home.logic.model.hospital.SectionResponseBean
import com.starry.home.logic.model.hospital.Ys
import com.starry.home.ui.adapter.SectionChildAdapter
import com.starry.home.ui.adapter.SectionParentAdapter
import com.starry.home.ui.adapter.SectionParentAdapter.OnItemClickListener
import com.starry.home.viewmodel.hospital.HospitalViewModel

/**
 * Package:com.starry.dotpen_patients.ui.hospital
 * Author:starr
 * Time:2024/1/4 20:47
 * Description: 科室和医生列表
 */
class HospitalActivity : BaseActivity<ActivityHospitalBinding, HospitalViewModel>() {

    private lateinit var sectionResponseBean: SectionResponseBean.Page
    private lateinit var doctorResponseBean: ArrayList<Ys?>
    private var searchList: MutableList<String> = mutableListOf()
    lateinit var rightAdapter: SectionChildAdapter
    private var leftAdapter: SectionParentAdapter? = null

    companion object {
        fun startAction(context: Context) {
            val intent = Intent(context, HospitalActivity::class.java)
            context.startActivity(intent)
        }
    }


    @Suppress("LABEL_NAME_CLASH")
    @SuppressLint("NotifyDataSetChanged")
    override fun initData() {
        searchList.addAll(mutableListOf("秦明", "柳宗元"))
        initSearchView()
        mViewModel.hospitalSection.observeForever {

            mDataBinding.rvParent.layoutManager = LinearLayoutManager(this)
            mDataBinding.rvChild.layoutManager = LinearLayoutManager(this@HospitalActivity)

            mViewModel.getSectionList(1)
            mViewModel.getDoctorList("内科")


            mViewModel.sectionList.observeForever {
                if (it.isSuccess) {
                    if (it.getOrNull() == null || it.getOrNull()?.page?.list?.size == 0) {
                        toast("未查询到科室信息")
                        return@observeForever
                    }
                    sectionResponseBean = it.getOrNull()?.page!!
                    sectionResponseBean.list.let { section ->
                        leftAdapter = SectionParentAdapter(section)
                        leftAdapter?.notifyDataSetChanged()
                        mDataBinding.rvParent.adapter = leftAdapter
                        leftAdapter?.setItemClick(object : OnItemClickListener {
                            override fun onItemClick(position: Int) {
                                doctorResponseBean.clear()
                                sectionResponseBean.list[position].emrKsName.let { sectionName ->
                                    mViewModel.getDoctorList(
                                        sectionName
                                    )
                                }
                                rightAdapter.notifyDataSetChanged()
                            }
                        })
                    }
                }
            }

            mViewModel.doctorList.observeForever {
                if (it.isSuccess) {
                    if (it.getOrNull() == null || it.getOrNull()?.ysList?.size == 0) {
                        toast("未查询到医生信息")
                        return@observeForever
                    }
                    doctorResponseBean = it.getOrNull()?.ysList!!
                    doctorResponseBean.let { doctor ->
                        rightAdapter = SectionChildAdapter(doctor)
                        rightAdapter.notifyDataSetChanged()
                        mDataBinding.rvChild.adapter = rightAdapter
                    }
                }
            }
        }

        mViewModel.ysInfo.observeForever {
            Log.i("lixu", "initData: ")
            if (it.isSuccess) {
                Log.i("lixu", "isSuccess: ")
                if (it.getOrNull() == null || it.getOrNull()?.pageBean?.list?.size == 0) {
                    toast("未查询到医生信息")
                    return@observeForever
                }
                mDataBinding.rvParent.visibility = View.GONE
                rightAdapter.updateData(it.getOrNull()?.pageBean?.list!!)
            }
        }




        mDataBinding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                mDataBinding.searchView.clearFocus()
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })


        mDataBinding.searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                mDataBinding.clSearchHistory.visibility = View.VISIBLE
            } else {
                mDataBinding.clSearchHistory.visibility = View.GONE
            }
        }

        mDataBinding.searchView.setOnCloseListener {
            mDataBinding.rvParent.visibility = View.VISIBLE
            false
        }


    }

    @SuppressLint("NotifyDataSetChanged")
    fun initSearchView() {
        val rvSearch = mDataBinding.rvSearchHistory
        val adapter = rvSearch.linear().setup {
            addType<String>(R.layout.item_search)
            onBind {
                val model = getModel<String>()
                val tvHistory = findView<TextView>(R.id.tvSearch)
                val tvSearch = findView<TextView>(R.id.tvSearch)
                tvSearch.singleClick {
                    mDataBinding.searchView.setQuery(tvSearch.text.toString(), true)
                    mViewModel.queryYsByName(tvSearch.text.toString())
                }
                tvHistory.text = model
            }
        }
        adapter.models = searchList
        val llClear = mDataBinding.llClear
        llClear.singleClick {
            searchList.clear()
            adapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        ZKStatusBarUtil.setTransparentForWindow(this)
        ZKStatusBarUtil.setDarkMode(this)
        mDataBinding.titleTopBar.baseIvBack.visibility = View.VISIBLE
        mDataBinding.titleTopBar.baseTvTitle.text = "科室和医生信息"
        mDataBinding.titleTopBar.baseIvBack.singleClick {
            finish()
        }
    }


    override fun getLayoutId() = R.layout.activity_hospital
}